/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taller1;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Andres
 */
public class Interfaz extends javax.swing.JFrame {

    /**
     * Creates new form InterfazTaller1
     */
    public Interfaz() {
        initComponents();
        tfNombre.setFont(new Font("Tahoma", Font.ITALIC, 11));
        sCherry.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                tfCherry.setText(String.valueOf(sCherry.getValue()));
            }
        });
        sCurlyMaple.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                tfCurlyMaple.setText(String.valueOf(sCurlyMaple.getValue()));
            }
        });
        sGenuineMahogany.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                tfGenuineMahogany.setText(String.valueOf(sGenuineMahogany.getValue()));
            }
        });
        sWenge.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                tfWenge.setText(String.valueOf(sWenge.getValue()));
            }
        });
        sWhiteOak.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e) {
                tfWhiteOak.setText(String.valueOf(sWhiteOak.getValue()));
            }
        });
        tfCherry.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(validarTextField(tfCherry)){
                    sCherry.setValue(Integer.parseInt(tfCherry.getText()));                    
                }
            }
        });
        tfCurlyMaple.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(validarTextField(tfCurlyMaple)){
                    sCurlyMaple.setValue(Integer.parseInt(tfCurlyMaple.getText()));                    
                }
            }
        });
        tfGenuineMahogany.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(validarTextField(tfGenuineMahogany)){
                    sGenuineMahogany.setValue(Integer.parseInt(tfGenuineMahogany.getText()));                    
                }
            }
        });
        tfWenge.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(validarTextField(tfWenge)){
                    sWenge.setValue(Integer.parseInt(tfWenge.getText()));                    
                }
            }
        });
        tfWhiteOak.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(validarTextField(tfWhiteOak)){
                    sWhiteOak.setValue(Integer.parseInt(tfWhiteOak.getText()));                    
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfCurlyMaple = new javax.swing.JTextField();
        tfCherry = new javax.swing.JTextField();
        sWhiteOak = new javax.swing.JSlider();
        sWenge = new javax.swing.JSlider();
        tfNombre = new javax.swing.JTextField();
        lNombre = new javax.swing.JLabel();
        sCherry = new javax.swing.JSlider();
        B_Pedido = new javax.swing.JButton();
        lGenuineMahogany = new javax.swing.JLabel();
        lCherry = new javax.swing.JLabel();
        lCurlyMaple = new javax.swing.JLabel();
        sGenuineMahogany = new javax.swing.JSlider();
        sCurlyMaple = new javax.swing.JSlider();
        lWhiteOak = new javax.swing.JLabel();
        lWenge = new javax.swing.JLabel();
        tfWenge = new javax.swing.JTextField();
        tfWhiteOak = new javax.swing.JTextField();
        tfGenuineMahogany = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tfCurlyMaple.setText("50");

        tfCherry.setText("50");

        sWhiteOak.setMaximum(500);

        sWenge.setMaximum(500);

        tfNombre.setText("por favor ingrese su nombre");
        tfNombre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tfNombreMouseClicked(evt);
            }
        });
        tfNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfNombreActionPerformed(evt);
            }
        });

        lNombre.setText("Nombre: ");

        sCherry.setMaximum(500);

        B_Pedido.setText("Solicitar Pedido");
        B_Pedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPedidoActionPerformed(evt);
            }
        });

        lGenuineMahogany.setText("Genuine Mahogany: ");

        lCherry.setText("Cherry:");

        lCurlyMaple.setText("Curly Maple:");

        sGenuineMahogany.setMaximum(500);

        sCurlyMaple.setMaximum(500);

        lWhiteOak.setText("White Oak:");

        lWenge.setText("Wenge:");

        tfWenge.setText("50");

        tfWhiteOak.setText("50");

        tfGenuineMahogany.setText("50");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lNombre)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 248, Short.MAX_VALUE)
                                .addComponent(B_Pedido))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lCherry)
                            .addComponent(lCurlyMaple)
                            .addComponent(lWhiteOak)
                            .addComponent(lWenge)
                            .addComponent(lGenuineMahogany))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sWhiteOak, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfWhiteOak))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sCherry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCherry))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sWenge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfWenge))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sGenuineMahogany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfGenuineMahogany))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sCurlyMaple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCurlyMaple)))))
                .addGap(27, 27, 27))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lNombre)
                            .addComponent(tfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lCherry)
                        .addGap(18, 18, 18)
                        .addComponent(lCurlyMaple)
                        .addGap(18, 18, 18)
                        .addComponent(lGenuineMahogany)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lWenge))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sCherry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfCherry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sCurlyMaple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfCurlyMaple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sGenuineMahogany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfGenuineMahogany, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfWenge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sWenge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(sWhiteOak, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lWhiteOak))
                    .addComponent(tfWhiteOak, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addComponent(B_Pedido)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfNombreActionPerformed

    private void bPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPedidoActionPerformed
        if(validarTfNombre()){
            // TODO add your handling code here:
        }
    }//GEN-LAST:event_bPedidoActionPerformed

    private void tfNombreMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tfNombreMouseClicked
        tfNombre.setText("");
        tfNombre.setFont(new Font("Tahoma", Font.PLAIN, 11));
    }//GEN-LAST:event_tfNombreMouseClicked
    
    private boolean validarTextField(JTextField tf) {
        try{
            Integer.parseInt(tf.getText());
            return true;
        }catch(NumberFormatException e){
            tf.setText("0");
            JOptionPane.showMessageDialog(null, "El número no es válido");            
            return false;
        }
    }
    
    private boolean validarTfNombre() {
        int length = tfNombre.getText().length();
        if(length > 4 && length < 20)
            return true;
        return false;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton B_Pedido;
    private javax.swing.JLabel lCherry;
    private javax.swing.JLabel lCurlyMaple;
    private javax.swing.JLabel lGenuineMahogany;
    private javax.swing.JLabel lNombre;
    private javax.swing.JLabel lWenge;
    private javax.swing.JLabel lWhiteOak;
    private javax.swing.JSlider sCherry;
    private javax.swing.JSlider sCurlyMaple;
    private javax.swing.JSlider sGenuineMahogany;
    private javax.swing.JSlider sWenge;
    private javax.swing.JSlider sWhiteOak;
    private javax.swing.JTextField tfCherry;
    private javax.swing.JTextField tfCurlyMaple;
    private javax.swing.JTextField tfGenuineMahogany;
    private javax.swing.JTextField tfNombre;
    private javax.swing.JTextField tfWenge;
    private javax.swing.JTextField tfWhiteOak;
    // End of variables declaration//GEN-END:variables
}
